package com.dnzx.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.dnzx.framework.page.PageResult;
import org.dnzx.framework.result.ResultUtils;
import org.dnzx.framework.result.domain.Result;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dnzx.client.IMenuClient;
import com.dnzx.client.IUserClient;
import com.dnzx.model.system.MenuBO;
import com.dnzx.model.system.UserBO;
import com.dnzx.security.JWTUserDetails;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/menu")
@Api(value = "/menu",description="菜单管理",produces=MediaType.APPLICATION_JSON_VALUE)
public class MenuController {
	
	@Resource
	private IMenuClient menuClient;
	@Resource
	private IUserClient userClient;
	
	/**
	 * 新增
	 * @param menuBO
	 * @return
	 */
//	@PostMapping(value = "/save")
    @ApiOperation(value = "/save",notes = "新增",httpMethod = "POST",produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<MenuBO> save(@ApiParam(value = "实体") @RequestBody @Valid MenuBO menuBO){
		try{
			this.menuClient.save(menuBO);
	        return ResultUtils.returnSuccess("SUCCESS",menuBO);
        }catch (Exception e){
            return ResultUtils.returnError("ERROR >>> " + e.getMessage());
        }
    }
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
//	@DeleteMapping(value = "/delete/{id}")
	@ApiOperation(value = "/delete/{id}",notes = "删除",httpMethod = "DELETE",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result delete(@ApiParam(value = "id") @PathVariable("id") Long id){
		try {
			this.menuClient.delete(id);
			return ResultUtils.returnSuccess("SUCCESS");
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 修改
	 * @param menuBO
	 * @return
	 */
//	@PutMapping(value = "/update")
	@ApiOperation(value = "/update",notes = "修改",httpMethod = "PUT",produces = MediaType.APPLICATION_JSON_VALUE)
	public Result<MenuBO> update(@ApiParam(value = "实体") @RequestBody MenuBO menuBO) {
		try {
			this.menuClient.update(menuBO); 
			return ResultUtils.returnSuccess("SUCCESS", menuBO);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查看
	 * @param id
	 * @return
	 */
//	@GetMapping("/get/{id}")
	@ApiOperation(value = "/get/{id}",notes = "查看",httpMethod = "GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result get(@ApiParam(value = "id") @PathVariable("id") Long id) {
		try {
			MenuBO menuBO = this.menuClient.get(id);
			return ResultUtils.returnSuccess("SUCCESS", menuBO);
		}catch(Exception e) {
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}

	/**
	 * 查询 - 分页
	 * @param menuBO
	 * @return
	 */
//	@PostMapping("/queryPage")
	@ApiOperation(value="/queryPage",notes="查询 - 分页",httpMethod="POST",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryPage(@ApiParam(value = "实体") @RequestBody MenuBO menuBO) {
		try {
			PageResult<List<MenuBO>> pageResult = new PageResult<List<MenuBO>>();
			pageResult.setCurrentPage(menuBO.getCurrentPage());
			pageResult.setPageSize(menuBO.getPageSize());
			pageResult = this.menuClient.queryPage(menuBO, pageResult);
			return ResultUtils.returnSuccess("SUCCESS", pageResult);
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查询 - 全部菜单树（用于权限分配）
	 * @param roleMenuBO
	 * @return
	 */
	@GetMapping("/queryTree")
	@ApiOperation(value="/queryTree",notes="查询 - 全部菜单树（用于权限分配）",httpMethod="GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryTree() {
		try {
			Long roleId = 1L;// 默认获取超级管理员的所有数据 TODO 后面需要去掉roleId这个参数
			MenuBO menuBO = this.menuClient.queryTree(roleId);
			return ResultUtils.returnSuccess("SUCCESS", menuBO.getSubMenuBOList());
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
	/**
	 * 查询 - 角色菜单树（用于菜单展示和权限分配）
	 * @param roleMenuBO
	 * @return
	 */
	@GetMapping("/queryTreeByRole/{roleId}")
	@ApiOperation(value="/queryTreeByRole/{roleId}",notes="查询 - 角色菜单树（用于菜单展示和权限分配）",httpMethod="GET",produces=MediaType.APPLICATION_JSON_VALUE)
	public Result queryTreeByRole(@ApiParam(value = "roleId") @PathVariable("roleId") Long roleId) {
		try {
//			System.out.println("oldRoleId:" + roleId);
			if (roleId == null || roleId == 0) {
				Authentication authen = SecurityContextHolder.getContext().getAuthentication();
				JWTUserDetails userDetails = (JWTUserDetails)authen.getPrincipal();
				UserBO userBO = this.userClient.get(userDetails.getUserId());
				roleId = userBO.getRoleId();
			}
//			System.out.println("newRoleId" + roleId);
			MenuBO menuBO = this.menuClient.queryTreeByRole(roleId);
			return ResultUtils.returnSuccess("SUCCESS", menuBO.getSubMenuBOList());
		}catch(Exception e) {
			e.printStackTrace();
			return ResultUtils.returnError("ERROR >>> " + e.getMessage());
		}
	}
	
}
